package com.erlinyou.travel.service.user.impl;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.erlinyou.base.service.BaseServiceImpl;
import com.erlinyou.im.common.util.StringUtil;
import com.erlinyou.im.dao.repository.goodfirend.GoodfirendRepository;
import com.erlinyou.travel.dao.repository.user.TUserRepository;
import com.erlinyou.travel.model.user.TUser;
import com.erlinyou.travel.service.user.IUserService;

/**
 *
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<TUser, Long> implements IUserService {
	private TUserRepository repository;
	@Resource
	private GoodfirendRepository goodfirendRepository;
	@Resource
	public void setBaseService(TUserRepository repository) {
		this.repository = repository;
		this.baseRepository = repository;
	}
	@Override
	public TUser findByUserid(Long userid) {
		return repository.findByUserid(userid);
	}
	@Override
	public TUser findByPhone(String phone) {
		return repository.findTopByPhone(phone, Sort.by(new org.springframework.data.domain.Sort.Order(Direction.DESC, "updateDate")));
	}
	@Override
	public List<TUser> selectByids(String ids,Long userid) {
		List<Map<String,Object>> rosterList = goodfirendRepository.findByUseridAndRelation1(userid,ids);
		List<Long> userIds = rosterList.stream().map(item -> Long.valueOf(item.get("jid").toString())).collect(Collectors.toList());
		return repository.findAllById(userIds);
	}
	
	@Override
	public List<TUser> selectByids(String gids) {
		return repository.selectByids(gids);
	}
	@Override
	public List<Map<String, Object>> searchByContacts(String userId, String nickName) {
		return repository.searchByContacts(userId,nickName);
	}
	@Override
	public List<Map<String, Object>> searchByboobuz(String nickName,int startPage,int rows) {
		return repository.searchByboobuz(nickName,startPage,rows);
	}
	@Override
	public List<Map<String, Object>> selectByUserid(String ids) {
		return repository.selectByUserid(ids);
	}
	
	
	@Override
	public List<Map<String, Object>> getInfoByPhone(String phones) {
		List<Map<String,Object>> list = repository.getInfoByPhone(phones);
		
		List<Map<String, Object>> resultlist = new ArrayList<Map<String, Object>>();
		
		for (Map<String,Object> map : list) {
			int userid = Integer.valueOf(map.get("userid").toString());
			String phone = map.get("phone").toString();
			String image = null;
			if(!map.containsKey("image")) {
				image = "";
			}else {
				image = map.get("image").toString();
			}
			String nickName = null;
			if(!map.containsKey("nickName")) {
				nickName = "";
			}else {
				nickName = map.get("nickName").toString();
			}
			int gender = Integer.valueOf(map.get("gender").toString());
			Map<String, Object> info=new HashMap<>();
			info.put("userid", userid);
			info.put("phone", phone);
			info.put("image", image);
			info.put("nickName", nickName);
			info.put("gender", gender);
			resultlist.add(info);
		}
		
		return resultlist;
	}
	@Override
	public int selectGenderByUsreId(Long userId) {
		
		
		return repository.selectGenderByUsreId(userId);
	}
	@Override
	public List<Map<String, Object>> randomUserInfo(List<Integer> rosterId, int gender) {
		String ids="";
    	for (int i=0;i<rosterId.size();i++) {
    		String item=rosterId.get(i).toString();
    		if(i==rosterId.size()-1){
    			item="'"+item+"'";
    		}else{
    			item="'"+item+"',";
    		}
    		ids+=item;
		}
    	int page=new Random().nextInt(10)+1;
    	List<Map<String, Object>> map = repository.randomUserInfo(ids,gender,page);
    	
    	List<Map<String, Object>> list = new ArrayList<>();
    	
    	for (Map<String, Object> map2 : map) {
    		int userid = Integer.valueOf(map2.get("userid").toString());
			String phone = map2.get("phone").toString();
			String image = map2.get("image").toString();
			String nickName = map2.get("nickName").toString();
			int dgender = Integer.valueOf(map2.get("gender").toString());
			Map<String, Object> info=new HashMap<>();
			info.put("userid", userid);
			info.put("phone", phone);
			info.put("image", image);
			info.put("nickName", nickName);
			info.put("gender", dgender);
			list.add(info);
		}
		return list;
	}
	
}
